package com.tsl.prevention.modules.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsl.prevention.modules.user.entity.Vaccine;
import com.tsl.prevention.modules.user.service.IVaccineService;
import com.tsl.prevention.util.Result;
import com.tsl.prevention.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户接种疫苗 前端控制器
 * </p>
 *
 * @author Lionel
 * @since 2021-03-20
 */
@RestController
@RequestMapping("/user/vaccine")
@Api
public class VaccineController {
    @Autowired
    IVaccineService iVaccineService;

    @PostMapping("/addVacc")
    @ApiOperation("接种时间")
    public Result addVacc(@RequestBody Vaccine vaccine) {
        //查询是否已经有用户
        vaccine.setId(StringUtil.getGUID());
        QueryWrapper<Vaccine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",vaccine.getUserId());
        Vaccine one = iVaccineService.getOne(queryWrapper);
        if (one != null) {
            iVaccineService.removeById(one.getId());
        }
        return Result.create(iVaccineService.save(vaccine));
    }
    @GetMapping("/getVacc")
    @ApiOperation("获取疫苗接种情况")
    public Result getVacc(String openId){
        QueryWrapper<Vaccine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",openId);
        Vaccine one = iVaccineService.getOne(queryWrapper);
        return Result.create(one);
    }

    @GetMapping("/delVacc")
    public Result delVacc(String openId) {
        iVaccineService.remove(new QueryWrapper<Vaccine>().eq("user_id",openId));
        return Result.create();
    }




}

